package Config;

import Entity.ChargingStations;
import Entity.InboundCounter;
import Entity.OutboundCounter;

import java.util.ArrayList;
import java.util.List;

public class GlobalVariables {
    // 数据库配置信息
    public static String URL;
    public static String USERNAME;
    public static String PASSWORD;
    // 网格尺寸信息
    public static int GRID_ROWS;
    public static int GRID_COLUMNS;
    public static int CELL_SIZE;
    // 出货柜台
    public static OutboundCounter outboundCounter1;
    public static OutboundCounter outboundCounter2;
    public static OutboundCounter outboundCounter3;
    public static OutboundCounter outboundCounter4;
    // 收货柜台
    public static InboundCounter inboundCounter1;
    public static InboundCounter inboundCounter2;
    public static InboundCounter inboundCounter3;
    public static InboundCounter inboundCounter4;
    // 充电站
    public static ChargingStations chargingStation1;
    public static ChargingStations chargingStation2;
    public static ChargingStations chargingStation3;
    public static ChargingStations chargingStation4;
    // 仓库配置
    public static WarehouseConfig config;
    // 网格列表
    public static List<List<Integer>> gridList = new ArrayList<>();


    // 初始化
    public static void init() {
        config = new WarehouseConfig();
        config.loadConfig("./config.txt");

        GRID_ROWS = config.getGridRows();
        GRID_COLUMNS = config.getGridColumns();
        CELL_SIZE = config.getCellSize();

        URL = config.getURL();
        USERNAME = config.getUsername();
        PASSWORD = config.getPassword();

        outboundCounter1 = new OutboundCounter(1, config.getOutboundPositions().get(0)[0], config.getOutboundPositions().get(0)[1]);
        outboundCounter2 = new OutboundCounter(2, config.getOutboundPositions().get(1)[0], config.getOutboundPositions().get(1)[1]);
        outboundCounter3 = new OutboundCounter(3, config.getOutboundPositions().get(2)[0], config.getOutboundPositions().get(2)[1]);
        outboundCounter4 = new OutboundCounter(4, config.getOutboundPositions().get(3)[0], config.getOutboundPositions().get(3)[1]);
        inboundCounter1 = new InboundCounter(1, config.getInboundPositions().get(0)[0], config.getInboundPositions().get(0)[1]);
        inboundCounter2 = new InboundCounter(2, config.getInboundPositions().get(1)[0], config.getInboundPositions().get(1)[1]);
        inboundCounter3 = new InboundCounter(3, config.getInboundPositions().get(2)[0], config.getInboundPositions().get(2)[1]);
        inboundCounter4 = new InboundCounter(4, config.getInboundPositions().get(3)[0], config.getInboundPositions().get(3)[1]);
        chargingStation1 = new ChargingStations(1, config.getChargingStationsPositions().get(0)[0], config.getChargingStationsPositions().get(0)[1]);
        chargingStation2 = new ChargingStations(2, config.getChargingStationsPositions().get(1)[0], config.getChargingStationsPositions().get(1)[1]);
        chargingStation3 = new ChargingStations(3, config.getChargingStationsPositions().get(2)[0], config.getChargingStationsPositions().get(2)[1]);
        chargingStation4 = new ChargingStations(4, config.getChargingStationsPositions().get(3)[0], config.getChargingStationsPositions().get(3)[1]);

        gridList = new ArrayList<>(); // 初始化网格列表
        initializeGrid();
    }

    private static void initializeGrid() {
        // 初始化为空闲单元格
        for (int row = 0; row < GRID_ROWS; row++) {
            List<Integer> rowList = new ArrayList<>();
            for (int col = 0; col < GRID_COLUMNS; col++) {
                rowList.add(0); // 空闲单元格
            }
            gridList.add(rowList);
        }

        // 设置书架位置
        for (int col = 2; col <= 28; col += 3) {
            for (int row = 1; row <= 17; row += 6) {
                for (int i = 0; i < 2; i++) { // 书架宽 2 列
                    for (int j = 0; j < 5; j++) { // 书架高 5 行
                        gridList.get(row + j).set(col + i, 1); // 书架
                    }
                }
            }
        }

        // 设置入库柜台位置
        for (int[] position : config.getInboundPositions()) {
            gridList.get(position[0]).set(position[1], 3); // 入库柜台
        }

        // 设置出库柜台位置
        for (int[] position : config.getOutboundPositions()) {
            gridList.get(position[0]).set(position[1], 4); // 出库柜台
        }

        // 设置充电桩位置
        for (int[] position : config.getChargingStationsPositions()) {
            gridList.get(position[0]).set(position[1], 5); // 充电桩
        }

        // 设置机器人位置
//        gridList.get(9).set(28, 5); // 机器人 A
//        gridList.get(12).set(1, 5); // 机器人 B
//        gridList.get(18).set(2, 5); // 机器人 C
    }

}
